package com.yipin.neimeng.util;


import com.yipin.neimeng.constant.HttpMethod;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

public class PostMethod implements HttpMethod {
    private HttpPost post;
    private CloseableHttpClient httpClient;

    public PostMethod(String url) {
        this.httpClient = HttpClients.createDefault();
        this.post = new HttpPost(url);
    }

    public void setRequestHeader(String headerName, String headerValue) {
        post.setHeader(headerName, headerValue);
    }

    public void setRequestEntity(String parameter, String contentType, String charset) throws Exception {
        StringEntity entity = new StringEntity(parameter, contentType, charset);
        post.setEntity(entity);
    }

    public void setRequestEntitys(StringEntity entity) throws Exception {
        post.setEntity(entity);
    }

    public String execute() throws Exception {
        HttpResponse response = (HttpResponse) httpClient.execute(post);
        return EntityUtils.toString(response.getEntity(), "UTF-8");
    }

    public void releaseConnection() {
        post.releaseConnection();
    }



    // Example usage
    public static void main(String[] args) {
        try {
            PostMethod postMethod = new PostMethod("http://218.202.142.37:11001/oppf");
            postMethod.setRequestHeader("Content-type", "application/json; charset=UTF-8");
            String busiParam = "{\"key\":\"6b968fcf48a19c399090e99a52747c61\"}";
            postMethod.setRequestEntity(busiParam, "application/json", "UTF-8");
            String response = postMethod.execute();
            System.out.println("Response: " + response);
            postMethod.releaseConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getResponseBodyAsString(){
        try {
            PostMethod postMethod = new PostMethod("http://218.202.142.37:11001/oppf");
            postMethod.setRequestHeader("Content-type", "application/json; charset=UTF-8");
            String busiParam = "{\"key\":\"6b968fcf48a19c399090e99a52747c61\"}";
            postMethod.setRequestEntity(busiParam, "application/json", "UTF-8");
            String response = postMethod.execute();
            System.out.println("Response: " + response);
            postMethod.releaseConnection();
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }


    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getUrl() {
        return null;
    }

    @Override
    public Map<String, String> getRequestHeaders() {
        return null;
    }

    @Override
    public boolean requiresRequestBody() {
        return false;
    }

    @Override
    public boolean permitsRequestBody() {
        return false;
    }

    @Override
    public void writeRequestBody(OutputStream out) throws IOException {

    }
}
